package com.fanketly.musicdiary.drag_test

import android.util.Log
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.lazy.LazyItemScope
import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.zIndex
import com.fanketly.musicdiary.data.TAG
import kotlin.math.abs

@Composable
fun rememberDragDropState(
    lazyListState: LazyListState,
    onSwap: (Int, Int) -> Unit
): DragDropState {
    val scope = rememberCoroutineScope()
    val state = remember(lazyListState) {
        DragDropState(
            state = lazyListState,
            onSwap = onSwap,
            scope = scope
        )
    }
    return state
}

fun LazyListState.getVisibleItemInfoFor(absoluteIndex: Int): LazyListItemInfo? {
    return this
        .layoutInfo
        .visibleItemsInfo
        .getOrNull(absoluteIndex - this.layoutInfo.visibleItemsInfo.first().index)
}

val LazyListItemInfo.offsetEnd: Int
    get() = this.offset + this.size

@ExperimentalFoundationApi
@Composable
fun LazyItemScope.DraggableItem(
    dragDropState: DragDropState,
    index: Int,
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.(isDragging: Boolean) -> Unit
) {
//    val current: Float by animateFloatAsState(dragDropState.draggingItemOffset * 0.67f)
    //排序位移幅度，此处设置会影响排序时画面移动的幅度和排序完成后item的动作幅度
    val current: Float by animateFloatAsState(dragDropState.draggingItemOffset * 0.67f)
    val previous: Float by animateFloatAsState(dragDropState.previousItemOffset.value * 0.67f)
    val dragging = index == dragDropState.currentIndexOfDraggedItem
//    Log.i(TAG, "DraggableItem:index:$index,dragging:$dragging ")
//    LaunchedEffect(key1 = dragDropState.draggingItemOffset, block = {
//
//    })
    val current2 = remember { mutableStateOf(0f) }
    current2.value = current
//    Log.i(TAG, "DraggableItem: ")
    //根据状态来设置Modifier
    val draggingModifier = if (dragging) {
        Modifier
            .zIndex(1f)
            .graphicsLayer {
                val a = current - current2.value
                val abs = abs(a)
                Log.i(TAG, "DraggableItem: $current _ ${current2.value}")
                Log.i(TAG, "DraggableItem_abs: $abs")
//                translationY = current
//                current2.value = translationY
                if (abs < 10) {
                    translationY = current
                    current2.value = translationY
                } else if (a > 0) {
//                    translationY = current - (abs-1)
//                    current2.value = translationY
                    translationY = current - abs
                    Log.i(TAG, "DraggableItem: a>0:${current - abs}")
                } else if (a < 0) {
                    Log.i(TAG, "DraggableItem: a<0:${current - abs}")
//                    translationY = current + (abs+2)
//                    current2.value = translationY
                }
                Log.i(TAG, "DraggableItem:y: ${translationY}")
            }
    } else if (index == dragDropState.previousIndexOfDraggedItem) {
        Modifier
            .zIndex(1f)
            .graphicsLayer {
                translationY = previous
            }
    } else {
        Modifier.animateItemPlacement(
            tween(easing = FastOutLinearInEasing)
        )
    }
    Column(modifier = modifier.then(draggingModifier)) {
        content(dragging)
    }
}